//
//  JinRiVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-18.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "JinRiVC.h"
#import "Goods.h"
#import "ShopCell.h"
#import "WSDropMenuView.h"
#define BASE_STRING @"http://www.shihuo.cn/app2/getYouhuiList?platform=ios&timestamp=1460368060&v=4.0.2&token=bcfa9679290c0c3f4eeb9ceed99bc176&page=%d&r=%d&sort=%@"
@interface JinRiVC () <UITableViewDataSource, UITableViewDelegate,WSDropMenuViewDataSource,WSDropMenuViewDelegate>
{
    int _r;
    NSString *_sort;
}


@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *nameArr;
@property (nonatomic, strong) NSArray *kindArr;

@property (nonatomic, strong) UIButton *newsBtn;
@property (nonatomic, strong) UIButton *kindBtn;

@property (nonatomic, assign) int page;
@property (nonatomic, assign) int r;
@property (nonatomic, copy) NSString *sort;
@end

@implementation JinRiVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    _dataArr = [[NSMutableArray alloc] init];
    _titleArr = @[@"new",@"hot"];
    _nameArr = @[@"最新发布",@"人气最高"];
    _kindArr = @[@"全部",@"运动户外",@"休闲鞋服",@"电脑数码",@"家用电器",@"食品酒水",@"家居百货",@"其他分类"];
    _page = 1;
    _r = 0;
    _sort = _titleArr[0];
    [self setBar];
    [self setMenu];
    [self getDataWithpage:_page r:_r sort:_sort];
    [self.view addSubview:self.tableView];
    [self setDropView];
    
}

#pragma mark -- 初始化tableView
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64+30, Width, Height-64-30) style:UITableViewStylePlain];
        _tableView.tag = 1000;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[ShopCell class] forCellReuseIdentifier:@"ShopCell"];
    }
    [self addPull];
    [self addInfinite];
    return _tableView;
}

#pragma mark -- UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopCell" forIndexPath:indexPath];
    cell.goods = _dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark -- 获取数据
- (void)getDataWithpage:(int)page r:(int)r sort:(NSString *)sort
{
    
    
    NSString *str = [NSString stringWithFormat:BASE_STRING,page,r ,sort];
    
    [MyRequest getWithURL:str parameters:nil success:^(NSDictionary *dic) {
        NSArray *arr = [dic objectForKey:@"data"];
    
        for (NSDictionary *dictionary in arr) {
            Goods *goods = [[Goods alloc] initWithDictionary:dictionary];
            [_dataArr addObject:goods];
        }
        [_tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)getData {
    
    NSString *str = [NSString stringWithFormat:BASE_STRING,1,_r ,_sort];
    
    [MyRequest getWithURL:str parameters:nil success:^(NSDictionary *dic) {
        NSArray *arr = [dic objectForKey:@"data"];
        [_dataArr removeAllObjects];
        for (NSDictionary *dictionary in arr) {
            Goods *goods = [[Goods alloc] initWithDictionary:dictionary];
            [_dataArr addObject:goods];
        }
        [_tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -- 下拉刷新
- (void)addPull
{
    __weak JinRiVC *mySelf = self;
    [_tableView addHeaderWithCallback:^{
        [mySelf getData];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [mySelf.tableView headerEndRefreshing];
        });
    }];
}
#pragma mark -- 上拉加载
- (void)addInfinite
{
    _page ++;
    __weak JinRiVC *mySelf = self;
    [_tableView addFooterWithCallback:^{
        [mySelf getDataWithpage:mySelf.page r:mySelf.r sort:mySelf.sort];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [mySelf.tableView headerEndRefreshing];
        });
    }];
}
#pragma mark -- WSDropMenuView
- (void)setDropView
{
    WSDropMenuView *dropMenu = [[WSDropMenuView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
    dropMenu.dataSource = self;
    dropMenu.delegate  =self;
    [self.view addSubview:dropMenu];
}
#pragma mark -- WSDropMenuViewDataSource,WSDropMenuViewDelegate

- (NSInteger)dropMenuView:(WSDropMenuView *)dropMenuView numberWithIndexPath:(WSIndexPath *)indexPath
{
    
    if (indexPath.column == 0) {
        return _nameArr.count;
    }else {
        return _kindArr.count;
    }
}
- (NSString *)dropMenuView:(WSDropMenuView *)dropMenuView titleWithIndexPath:(WSIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        return _nameArr[indexPath.row];
    }else {
        return _kindArr[indexPath.row];
    }
}
- (void)dropMenuView:(WSDropMenuView *)dropMenuView didSelectWithIndexPath:(WSIndexPath *)indexPath
{
    _dataArr = [[NSMutableArray alloc] init];
    if (indexPath.column == 1) {
        _r = (int)indexPath.row;
    }
    if (indexPath.column == 0) {
        _sort = _titleArr[indexPath.row];
    }
    _page = 1;
    [self getDataWithpage:_page r:_r sort:_sort];
    NSLog(@"%d,%d,%@",_page,_r,_sort);
}
#pragma mark -- 创建导航
- (void)setBar
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 64)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:bgView.frame];
    imageView.image = [UIImage imageNamed:@"topbar"];
    [bgView addSubview:imageView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 20, 50, 40);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"F_iocn_normal"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:backBtn];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectZero;
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"D_iocn_normal"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:menuBtn];
    
    [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(20);
        make.right.offset(-10);
        make.width.offset(50);
        make.height.offset(40);
    }];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLab.text = @"识货优惠";
    titleLab.font = [UIFont systemFontOfSize:20];
    [bgView addSubview:titleLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.centerY.equalTo(bgView.mas_centerY).offset(10);
    }];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showMenu
{
    static int count = 1;
    if (count%2 == 1) {
        
        [self showView];
    }else
    {
        [self hideView];
    }
    count ++;
}
- (void)setMenu
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(Width*2/3, 64, Width/3, 0)];
    _bgView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_bgView];
    
    NSArray *arr = @[@"首页",@"我的",@"消息"];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 30*i, Width/3, 30);
        button.tag = i;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:button];
        _bgView.clipsToBounds = YES;
    }
}
- (void)showView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _bgView.frame = CGRectMake(Width*2/3, 64, Width/3, 90);
    [UIView commitAnimations];
}
- (void)hideView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _bgView.frame = CGRectMake(Width*2/3, 64, Width/3, 0);
    [UIView commitAnimations];
}
- (void)clickButton:(UIButton *)button
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
